#!/usr/bin/env python3
############################################################################
# tools/mkconfig.py
#
#   Copyright 2018 Sony Semiconductor Solutions Corporation
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in
#    the documentation and/or other materials provided with the
#    distribution.
# 3. Neither the name of Sony Semiconductor Solutions Corporation nor
#    the names of its contributors may be used to endorse or promote
#    products derived from this software without specific prior written
#    permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
############################################################################

import os
import sys
import re

# DEQUOTE_SYMBOLS is listed configuration variable name that are quoted by
# configuration tool but which must be unquoted when used in C code.

DEQUOTE_SYMBOLS = ["CONFIG_SDK_USER_ENTRYPOINT"]

# RESERVED_SYMBOLS is listed configuration definitions for avoid multiple
# definition warnings.
# For example, CONFIG_SENSORS exists both of source trees (nuttx and sdk).
# It just add '#ifndef' and '#endif' around the reserved symbols.
# The 'CONFIG_' prefix is automatically appends at the runtime.

RESERVED_SYMBOLS = ["SENSORS"]

if 'TOPDIR' in os.environ:
    topdir = os.path.abspath(os.environ['TOPDIR'])
else:
    topdir = os.getcwd()

configfile = os.path.join(topdir, '.config')
if not os.path.exists(configfile):
    print(".config file not found.", file=sys.stderr)
    sys.exit(1)

# Extract symbols

syms = []
for sym in RESERVED_SYMBOLS:
    syms.append("CONFIG_" + sym)
 
# Now let's convert each configurations to C source header

print("/* This header generated by tools/mkconfig.py. Do not edit. */")
print()
print("#include <nuttx/config.h>")
print()
print("#ifndef __BSP_INCLUDE_SDK_CONFIG_H")
print("#define __BSP_INCLUDE_SDK_CONFIG_H")
print()

with open(configfile, 'r') as f:
    for line in f:
        line = re.sub(r'(.*)#.*', r'\1', line).strip()
        if len(line) == 0:
            continue
        
        m = re.match(r'(?P<name>.*)=(?P<value>.*)', line)
        if not m:
            print("Illegal config format.", file=sys.stderr)
            sys.exit(2)
        name = m.group('name')
        value = m.group('value')

        if name in DEQUOTE_SYMBOLS:
            value = value[1:-1]

        isreserved = name in syms
        if isreserved:
            print("#ifndef", name)
            if value == 'y':
                print("#  define", name, '1')
            else:
                print("#  define", name, value)
            print("#endif")
        else:
            if value == 'y':
                print("#define", name, '1')
            else:
                print("#define", name, value)

print()

# Sanity checks

print("#endif /* __BSP_INCLUDE_SDK_CONFIG_H */")
